package secrpc

import (
	"dub/define/grpcmodel"
	"dub/utils"
	"fmt"
	"golang.org/x/net/context"
	"regexp"
)

type SecRpcRegion struct {
	log   *utils.Logger
	dbRpc *utils.RpcProxy
}

//查询级别有关的行政区信息
func (s *SecRpcRegion) Find(ctx context.Context, args *grpcmodel.ModelSecRpcRegionReqFind) (reply *grpcmodel.ModelSecRpcRegionResFind, err error) {
	reply = &grpcmodel.ModelSecRpcRegionResFind{}
	findMap := make(map[uint8]string)
	if args.GetRegionCode() == "" {
		reply.DataType = 0
		findMap[uint8(1)] = ""
	} else {
		length_regionCode := len(args.GetRegionCode())
		isFindTwo, _ := regexp.MatchString("^[1-9][0-9]{1}0000", args.GetRegionCode())
		if isFindTwo && length_regionCode == 6 {
			reply.DataType = 1
			findMap[uint8(2)] = args.GetRegionCode()
		} else {
			isFindThi, _ := regexp.MatchString("^[1-9][0-9]{3}00", args.GetRegionCode())
			if isFindThi && length_regionCode == 6 {
				reply.DataType = 2
				findMap[uint8(3)] = args.GetRegionCode()
			} else {
				//如果是第三级则全部查询
				reply.DataType = 3
				findMap[uint8(1)] = ""
				findMap[uint8(2)] = fmt.Sprintf("%s0000", args.GetRegionCode()[:2])
				findMap[uint8(3)] = fmt.Sprintf("%s00", args.GetRegionCode()[:4])
			}
		}
	}

	for {
		if len(findMap) < 1 {
			break
		}

		db_region := grpcmodel.NewIDbRpcRegionClient(s.dbRpc.Conn())
		for key, val := range findMap {
			db_args := &grpcmodel.ModelDbRpcRegionReqFind{
				RegionLevel: uint32(key),
				PreCode:     val,
			}
			s.log.Infof("def_func_sec_region.go Find method db_region.Find ad_args info is %+v\n", db_args)
			db_reply, err := db_region.Find(s.dbRpc.Context(), db_args)
			s.log.Infof("def_func_sec_region.go Find method db_region.Find ad_reply info is %+v\n", db_reply)
			if err != nil {
				s.log.Errorf("def_func_sec_region.go Find method db_region.Find err. %v\n", err)
				reply.Err = 2
				break
			}
			reply.Err = db_reply.GetErr()
			if reply.Err == 2 {
				break
			}

			if len(db_reply.GetRows()) < 1 {
				continue
			}
			resions := make([]*grpcmodel.ModelSecRpcRegion, 0, len(db_reply.GetRows()))
			for _, res := range db_reply.GetRows() {
				resions = append(resions, &grpcmodel.ModelSecRpcRegion{
					RegionName: res.GetRegionName(),
					RegionCode: res.GetRegionCode(),
				})
			}

			if key == 1 {
				reply.FirResions = resions
			}
			if key == 2 {
				reply.TwoResions = resions
			}
			if key == 3 {
				reply.ThiResions = resions
			}
		}
		break
	}
	return
}

//以键值形式返回所有的行政区域信息
func (s *SecRpcRegion) regionMaps() map[string]string {
	//把查询出来的区域信息转化为键值信息
	region_map := make(map[string]string)
	for {
		//查询所有的行政区域信息
		re_arg := &grpcmodel.ModelDbRpcRegionReqFind{
			IsFindAll: true,
		}
		db_region := grpcmodel.NewIDbRpcRegionClient(s.dbRpc.Conn())
		s.log.Infof("def_func_sec_region.go regionMaps method db_region.Find re_arg info is %+v\n", re_arg)
		re_reply, err := db_region.Find(s.dbRpc.Context(), re_arg)
		s.log.Infof("def_func_sec_region.go regionMaps method db_region.Find re_reply info is %+v\n", re_reply)
		if err != nil {
			s.log.Errorf("def_func_sec_region.go regionMaps method db_region.Find err. %v\n", err)
			break
		}
		if re_reply.GetErr() >= 1 {
			break
		}
		for _, region := range re_reply.GetRows() {
			region_map[region.GetRegionCode()] = region.GetRegionName()
		}
		break
	}
	return region_map
}

//根据第三级得到所有的行政区组合
func (s *SecRpcRegion) allRegionName(region_map map[string]string, regionCode string) string {
	regionName := ""

	first_code := fmt.Sprintf("%s0000", regionCode[:2])
	first_name, first_ok := region_map[first_code]
	if first_ok {
		regionName = fmt.Sprintf("%s %s", regionName, first_name)
	}

	two_code := fmt.Sprintf("%s00", regionCode[:4])
	two_name, two_ok := region_map[two_code]
	if two_ok {
		regionName = fmt.Sprintf("%s %s", regionName, two_name)
	}

	thir_name, thir_ok := region_map[regionCode]
	if thir_ok {
		regionName = fmt.Sprintf("%s %s", regionName, thir_name)
	}
	return regionName
}

var _secRpcRegion *SecRpcRegion

func NewSecRpcRegion(log *utils.Logger, dbRpc *utils.RpcProxy) *SecRpcRegion {
	if _secRpcRegion == nil {
		_secRpcRegion = &SecRpcRegion{
			log:   log,
			dbRpc: dbRpc,
		}
	}
	return _secRpcRegion
}
